package com.itheima.reggie.Service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.Mapper.CategoryMapper;
import com.itheima.reggie.Pojo.Category;
import com.itheima.reggie.Pojo.Dish;
import com.itheima.reggie.Pojo.Setmeal;
import com.itheima.reggie.Service.CategoryService;
import com.itheima.reggie.Service.DishService;
import com.itheima.reggie.Service.SetmealService;
import com.itheima.reggie.common.CustomExcepction;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class CategoryServiceImpl extends ServiceImpl<CategoryMapper, Category>
                                 implements CategoryService{

    @Autowired
    private DishService diservice;

    @Autowired
    private SetmealService setservice;

    @Override
    public void removeid(Long id) {
    //1、查询该分类有无菜品关联
        LambdaQueryWrapper<Dish> queryWrapper = new LambdaQueryWrapper<>();
    // 添加查询条件，根据分类id(categoryId)进行查询
        queryWrapper.eq(Dish::getCategoryId,id);
        int count = diservice.count(queryWrapper);
        if(count>0){
    //  已经关联菜品，抛出一个业务异常
            throw new CustomExcepction("该分类已关联菜品，无法删除");
        }

    //2、查询该分类有无套餐关联
        LambdaQueryWrapper<Setmeal> queryWrapper2 = new LambdaQueryWrapper<>();
    // 添加查询条件，根据分类id(categoryId)进行查询
        queryWrapper2.eq(Setmeal::getCategoryId,id);
        int count2 = setservice.count(queryWrapper2);
        if(count2>0){
    // 已经关联套餐，抛出一个业务异常
            throw new CustomExcepction("该分类已关联套餐，无法删除");
        }

    //3、没有菜品或套餐关联，可以删除
        removeById(id);

    }
}
